<?php

/**
 * folder_tools actions.
 *
 * @package    sf_portal
 * @subpackage folder_tools
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class folder_toolsActions extends sfActions {
	/**
	 * Открытие каталога
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeOpen_folder(sfWebRequest $request) {
		
		$this->cur_f = FolderPeer::retrieveByPK ( $request->getParameter ( 'id' ) );
		$this->folders = $this->cur_f->getChildren ();
		
		///Запоминаем открытие
		$rem = new Remember();
		$rem -> setClassName('Folder');
		$rem -> setItemId($this->cur_f->getId());
		$rem->setUserId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
		$rem->save();
	
	}

	

	
	/**
	 * Закрытие каталога
	 *
	 * 
	 */
	public function executeClose_folder(sfWebRequest $request) {
		
		$this->cur_f = FolderPeer::retrieveByPK ( $request->getParameter ( 'id' ) );
		//$this->folders = $this->cur_f->getChildren ();
		
		///Запоминаем закрытие
		$c = new Criteria();
		$c->add(RememberPeer::USER_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
		$c->addAnd(RememberPeer::ITEM_ID,$request->getParameter ( 'id' ));
		$c->addAnd(RememberPeer::CLASS_NAME,'Folder');
		RememberPeer::doDelete($c);
		if($this->cur_f->hasChildren())
		{
			$ch = $this->cur_f->getChildren();
			foreach($ch as $h)
			{
				$c = new Criteria();
				$c->add(RememberPeer::USER_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
				$c->addAnd(RememberPeer::ITEM_ID,$h->getId());
				$c->addAnd(RememberPeer::CLASS_NAME,'Folder');
				RememberPeer::doDelete($c);
			}
		}
	
	
	}
	
	
	/*
   * Форма добавления редактирования каталога
   */
	public function executeShow_folder_form() {
		
		if ($this->getRequestParameter ( 'near' ) == 'true') {
			$this->near = 'true';
		}
		
		$f = FolderPeer::retrieveByPk ( $this->getRequestParameter ( 'id' ) );
		
		//if (! $f)
		//	$this->forward ( 'mycontent', 'list' );
		
			if($this->getRequestParameter ( 'edit_id' ))
			{
			  $fl = FolderPeer::retrieveByPk ( $this->getRequestParameter ( 'edit_id' ) );
			  $this->form = new simpleFolderForm (  $fl );	
			}
			else 
			{
			  $this->form = new simpleFolderForm ( );
			}
		
		$this->root = $this->getRequestParameter ( 'id' );
		$this->setLayout ( false );
		sfConfig::set ( 'sf_web_debug', false );
	}
	
	public function executeMove_folder_up(sfWebRequest $reques) {
		
		$node = FolderPeer::retrieveByPK ( $this->getRequestParameter ( 'node' ) );
		$this->cur_f = $node->retrieveParent() ;
		$prev = $node->retrievePrevSibling ( );
		$node->moveToPrevSiblingOf ( $prev );
		$node->save ();
		$this->setLayout ( false );
		sfConfig::set ( 'sf_web_debug', false );
		$this->setTemplate ( 'open_folder' );
		 ////Выбираем  дочерние каталоги 
        $this->folders= $this->cur_f->getChildren ();
		
	}
	
	public function executeMove_folder_down(sfWebRequest $reques) {
		
		$node = FolderPeer::retrieveByPK ( $this->getRequestParameter ( 'node' ) );
		$this->cur_f = $node->retrieveParent() ;
		$prev = $node->retrieveNextSibling ( );

		$node->moveToNextSiblingOf ( $prev );
		$node->save ();
		$this->setLayout ( false );
		sfConfig::set ( 'sf_web_debug', false );
		$this->setTemplate ( 'open_folder' );
		 ////Выбираем  дочерние каталоги 
        $this->folders= $this->cur_f->getChildren ();
	}

	public function executeDelete_folder() {
		
		$f = FolderPeer::retrieveByPk ( $this->getRequestParameter ( 'id' ) );
		$this->cur_f= $f->retrieveParent();
		
		if ($f)
			$f->delete ();		
		$this->redirect('gift/index');
	
	}
	
   public function executeSave_folder(sfWebRequest $request) {
		
  
    if ($request->isMethod('post'))

    {
       $fl = FolderPeer::retrieveByPK($request->getParameter('folder[id]'));	
      
       if(!$fl)
       {
       	  $froot = FolderPeer::retrieveByPk ( $this->getRequestParameter ( 'root' ) );
		        $fl = new Folder ( );
				if($request->getParameter('near')=='true')
				{
					$fl->insertAsNextSiblingOf ( $froot );
				}
				else 
				{
					$fl->insertAsLastChildOf ( $froot );
				}
       }
       $this->form = new FolderForm($fl);
      $this->form->bind($request->getParameter('folder'), $request->getFiles('folder'));
      if ($this->form->isValid())
      {
         $this->form->save(); 
         $this->form->getObject()->setUserId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
         $this->form->getObject()->save();   
      }

    }
	
    ///Открываем каталог
        if(isset($froot))
        {
		$rem = new Remember();
		$rem -> setClassName('Folder');
		$rem -> setItemId($froot->getId());
		$rem->setUserId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
		$rem->save();
        }
		//////
    //$this->setTemplate('show_folder_form');
    $this->redirect ( 'gift/index' );
    
	}
	

	
}
